using System;
using NUnit.Framework;
using Nuwanda.TfsSpotlight.Data;
using Nuwanda.TfsSpotlight.Shell.Services.Interfaces;
using Nuwanda.TfsSpotlight.TFS;
using Rhino.Mocks;

namespace TfsSpotlight.Tests.TFS
{
    [TestFixture]
    public class TfsServerRepositoryTests
    {
        private MockRepository _mocks;
        private IObjectFactory _objectFactoryMock;

        private TfsServerRepositoryTestableStub _testSubject;

        [SetUp]
        public void SetUp()
        {
            this._mocks = new MockRepository();
            this._objectFactoryMock = this._mocks.StrictMock<IObjectFactory>();

            this._testSubject = new TfsServerRepositoryTestableStub(this._objectFactoryMock);
        }

        //[Test, ExpectedException(typeof(ArgumentNullException))]
        //public void WhenAskedTo_GetServer_And_PassedNullParameter_Should_ThrowArgumentNullException()
        //{
        //    this._testSubject.GetServer();
        //}

        [Test]
        public void WhenAskedTo_GetServer_And_SuchServerExists_And_HasBeenRetrievedAlready_Should_ReturnInstanceFromCache()
        {
            var serverUrl = "url";
            var tfsManagerStub = this._mocks.Stub<ITfsServerManager>();
            this._testSubject.Test_Managers.Add(serverUrl, tfsManagerStub);
            var serverToRetrieve = new ServerDescriptor("name", serverUrl);

            var result = this._testSubject.GetServer(serverToRetrieve);

            Assert.AreSame(tfsManagerStub, result);
        }

        [Test]
        public void WhenAskedTo_GetServer_And_SuchServerExists_And_HasNotBeenRetrievedYet_Should_AddRetrievedServersToInternalList()
        {
            var serverUrl = "url";
            var tfsManagerToCreate = this._mocks.Stub<ITfsServerManager>();
            var serverToRetrieve = new ServerDescriptor("name", serverUrl);

            using (this._mocks.Record())
            {
                Expect.Call(this._objectFactoryMock.GetInstance<ITfsServerManager>()).Return(tfsManagerToCreate);
            }

            using (this._mocks.Playback())
            {
                var result = this._testSubject.GetServer(serverToRetrieve);
                Assert.AreSame(tfsManagerToCreate, result);
                Assert.AreSame(serverToRetrieve, result.ServerDescriptor);
                Assert.IsTrue(this._testSubject.Test_Managers.ContainsKey(serverUrl));
                Assert.IsTrue(this._testSubject.Test_Managers.ContainsValue(tfsManagerToCreate));
            }
        }
    }
}